<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader :arr="list" @create="createFn"></TodoHeader>
    <TodoMain :arr="showArr" @delList="delFn"></TodoMain>
    <TodoFooter
      :farr="showArr"
      @changeType="typeFn"
      @clearF="clearFn"
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css";
import "./styles/index.css";

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      // list: [
      //   { id: 100, name: "穿山甲", isDone: true },
      //   { id: 201, name: "屑司令", isDone: false },
      //   { id: 103, name: "毒鸡汤", isDone: true },
      // ],
      list: JSON.parse(localStorage.getItem("todoList")) || [],
      getSel: "all",
    };
  },
  methods: {
    createFn(taskName) {
      let id =
        this.list.length == 0 ? 100 : this.list[this.list.length - 1].id + 1;
      this.list.push({
        id: id,
        name: taskName,
        isDone: false,
      });
    },
    delFn(index) {
      // let index = this.list.findIndex((obj) => obj.id === ind);
      this.list.splice(index, 1);
    },
    typeFn(str) {
      this.getSel = str;
    },
    clearFn() {
      this.list = this.list.filter((obj) => obj.isDone === false);
    },
  },
  computed: {
    showArr() {
      if (this.getSel === "yes") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.getSel === "no") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("todoList", JSON.stringify(this.list));
      },
    },
  },
};
</script>